<template>
  <div>
    <div class="app-container" style="height: 100%">
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="地块和站点名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入地块和站点名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-button
          @click="setxsFAFun"
          style="margin-left: 20px"
          type="success"
          icon="Connection"
          v-loading="modalLoading"
          >生成需水模型方案</el-button
        >

        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="tableData">
        <el-table-column
          width="150px"
          prop="name"
          align="center"
          label="地块名称"
        ></el-table-column>
        <el-table-column
          prop="alpha"
          align="center"
          width="200px"
          label="降雨有效利用系数 (入渗系数)"
        ></el-table-column>
        <el-table-column prop="area" align="center" label="地块面积" />
        <el-table-column
          prop="cropsName"
          width="120px"
          align="center"
          label="农作物名称"
        />
        <el-table-column
          prop="delta"
          align="center"
          width="200px"
          label="饱和水汽压与温度曲线的斜率 (hPa/℃)"
        />
        <el-table-column
          prop="eaEd"
          align="center"
          width="200px"
          label="饱和蒸汽压与实际蒸汽压差 (hPa)"
        />
        <el-table-column
          prop="etaField"
          align="center"
          width="150px"
          label="田间灌溉水利用系数"
        />
        <el-table-column
          prop="gamma"
          align="center"
          width="150px"
          label="湿度计常数 (hPa/℃)"
        />
        <el-table-column
          prop="gmd"
          align="center"
          width="150px"
          label="土壤热通量 (MJ/m²d)"
        />
        <el-table-column prop="kc" align="center" label="作物系数" />

        <el-table-column prop="maxCapacity" align="center" label="最大容量" />

        <el-table-column
          prop="rainfall"
          align="center"
          label="单次降雨量 (mm)"
        />
        <el-table-column
          prop="rn"
          align="center"
          width="200px"
          label="作物表面的太阳净辐射"
        />
        <el-table-column prop="tpjwd" align="center" label="平均气温 (℃)" />
        <el-table-column prop="u2fs" align="center" label="2米处风速 (m/s)" />
        <el-table-column
          label="操作"
          align="center"
          width="150px"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >

            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        :title="title"
        v-model="open"
        width="1200px"
        append-to-body
        destroy-on-close
      >
        <el-form ref="formRef" :model="form" label-width="260px">
          <!-- 第一行 -->
          <el-divider content-position="left">基本参数</el-divider>
          <el-row>
            <el-col :span="16">
              <el-form-item label="地块和站点名称" prop="name">
                <el-input
                  v-model="form.name"
                  placeholder="请输入地块和站点名称"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="农作物名称" prop="cropsId">
                <el-select
                  v-model="form.cropsId"
                  placeholder="请选择农作物名称"
                >
                  <el-option
                    v-for="item in cropsList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第二行 -->
          <el-divider content-position="left">需水模型参数</el-divider>
          <el-row>
            <el-col :span="8">
              <el-form-item label="2 米处风速 (m/s)" prop="u2fs">
                <el-input-number
                  :min="0.001"
                  v-model="form.u2fs"
                  placeholder="请输入 2 米处风速"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="田间灌溉水利用系数" prop="etaField">
                <el-input-number :min="0.001" v-model="form.etaField" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第三行 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="单次降雨量 (mm)" prop="rainfall">
                <el-input-number
                  :min="0.001"
                  v-model="form.rainfall"
                  placeholder="请输入单次降雨量"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="降雨有效利用系数（入渗系数）" prop="alpha">
                <el-input-number
                  :min="0.001"
                  v-model="form.alpha"
                  placeholder="请输入降雨有效利用系数"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="地块面积" prop="area">
                <el-input-number
                  :min="0.001"
                  v-model="form.area"
                  placeholder="请输入地块面积"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第五行 -->
          <el-row>
            <el-col :span="8">
              <el-form-item
                label="饱和水汽压与温度曲线的斜率 (hPa/℃)"
                prop="delta"
              >
                <el-input-number :min="0.001" v-model="form.delta" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="饱和蒸汽压与实际蒸汽压差 (hPa)" prop="eaEd">
                <el-input-number :min="0.001" v-model="form.eaEd" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第六行 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="湿度计常数 (hPa/℃)" prop="gamma">
                <el-input-number
                  :min="0.001"
                  v-model="form.gamma"
                  placeholder="请输入湿度计常数"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="土壤热通量 (MJ/m²d)" prop="gmd">
                <el-input-number
                  :min="0.001"
                  v-model="form.gmd"
                  placeholder="请输入土壤热通量"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="平均气温 (℃)" prop="tpjwd">
                <el-input-number
                  :min="0.001"
                  v-model="form.tpjwd"
                  placeholder="请输入平均气温"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>

      <!-- 分页区域 -->
      <el-pagination
        style="float: right"
        @current-change="handleCurrentChange"
        :current-page="pagination.curPage"
        :page-size="pagination.pageSize"
        layout="total, prev, pager, next"
        :total="pagination.total"
      />
    </div>
    <faDetail ref="faDetailRef" />
  </div>
</template>

<script setup>
import faDetail from "@/views/modal/faDetail.vue";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import {
  getDkbzList,
  delDkbz,
  getDkbzDetailById,
  saveDkbz,
  setxsFA,
  setssFA,
  setyhsFA,
} from "@/api/modal/dkbz";
import { getNzwList } from "@/api/modal/nzw";
import { ElMessage, ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const tableData = ref([]);
const open = ref(false);
const loading = ref(true);
let cropsList = reactive([]);
const showSearch = ref(false);
const modalLoading = ref(false);
const title = ref("");
const pagination = reactive({
  curPage: 1,
  pageSize: 10,
  total: 0,
});
const faDetailRef = ref();
const data = reactive({
  queryParams: {
    name: "",
  },
  rules: {
    name: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
  },
});
const { queryParams } = toRefs(data);
const form = reactive({});
getNzwList({ pageSize: 100, curPage: 1 }).then((res) => {
  cropsList = res.data.list;
});
const typeList = reactive([
  { label: "地块和泵站", value: 0 },
  { label: "水源 ", value: 1 },
  { label: "渠道 ", value: 2 },
]);
function handleCurrentChange(val) {
  pagination.curPage = val;
  getList();
}
function getNodeType(type) {
  return typeList.find((item) => item.value === type)?.label;
}
// 更新重置方法，初始化所有字段
function reset() {
  Object.assign(form, {
    id: null,
    name: "",
    cropsId: null,
    irriId: 1000,
    cropsName: "",
    alpha: 0,
    area: 0,
    delta: 0,
    eaEd: 0,
    etaField: 0,
    gamma: 0,
    gmd: 0,
    maxCapacity: 0,
    rainfall: 0,
    tpjwd: 0,
    u2fs: 0,
  });
  proxy.resetForm("formRef");
}
function setxsFAFun() {
  modalLoading.value = true;
  setxsFA({ irriId: 1000 }).then((res) => {
    ElMessage.success("生成成功!");
    getList();
    modalLoading.value = false;
    faDetailRef.value.openDialog(res.data, "输水模型方案");
  });
}
/** 查询列表 */
function getList() {
  loading.value = true;
  getDkbzList({ ...pagination, ...queryParams.value }).then((response) => {
    tableData.value = response.data.list;
    pagination.total = response.data.totalCount;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置搜索条件 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加地块与模型参数";
}

/** 修改按钮 */
function handleUpdate(row) {
  getDkbzDetailById(row.id).then((res) => {
    Object.assign(form, res.data);
    open.value = true;
    title.value = "修改添加地块与模型参数";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      saveDkbz(form).then(() => {
        proxy.$modal.msgSuccess(form.id ? "修改成功" : "新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮 */
function handleDelete(row) {
  ElMessageBox.confirm(
    '是否确认删除名称为"' + row.name + '"的数据项?',
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      delDkbz([row.id]).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      });
    })
    .catch(() => {
      ElMessage.info("取消删除");
    });
}

getList();
</script>

<style lang="scss" scoped>
:deep(.el-divider__text) {
  font-size: 16px;
  font-weight: bold;
  color: rgb(42, 114, 249);
}
.avatar-uploader .avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader :deep(.el-upload:hover) {
  border-color: var(--el-color-primary);
}
:deep(.el-icon.avatar-uploader-icon) {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
}
</style>
